package ui.activity.mine.presenter;


import com.google.gson.Gson;

import javax.inject.Inject;

import base.BaseBiz;
import model.PersonalCenterBean;
import model.PersonalCenterParamBean;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import ui.activity.mine.PhoneSuccessAct;
import ui.activity.mine.biz.PhoneBiz;
import ui.activity.mine.contract.PhoneContract;
import ui.model.EditPhoneBean;
import util.StringUtils;

/**
 * Created by chenjian on 19/2/26
 */

public class PhonePresenter implements PhoneContract.Presenter {

    PhoneContract.View view;
    PhoneBiz phoneBiz;
    String userPhone = "";


    @Inject
    public PhonePresenter(PhoneContract.View view) {
        this.view = view;
    }


    @Override
    public void setBiz(BaseBiz biz) {
        phoneBiz = (PhoneBiz) biz;
    }

    @Override
    public void getCode(String phone) {
        if (phone.equals(userPhone)) {
            view.showMsg("手机号与默认手机号相同");
            return;
        }
        if (StringUtils.isEmpty(phone)) {
            view.showMsg("请输入手机号码");
            return;
        }
        view.loading(true);
        phoneBiz.getCode(phone, new BaseBiz.Callback<Object>() {
            @Override
            public void onSuccess(Object info) {
                view.showMsg("发送验证码成功");
                view.upDataCode();
                view.loading(false);
            }

            @Override
            public void onFailure(Object info) {
                view.loading(false);
            }
        });
    }

    @Override
    public void submitData(String phone, String pwd, String validateCode) {
        if (StringUtils.isEmpty(phone)) {
            view.showMsg("请输入手机号码");
            return;
        }
        if (StringUtils.isEmpty(validateCode)) {
            view.showMsg("请输入验证码");
            return;
        }
        EditPhoneBean p = new EditPhoneBean();
        p.setContent(phone);
        p.setValidateCode(validateCode);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), new Gson().toJson(p));
        phoneBiz.setPwd(requestBody, new BaseBiz.Callback<Boolean>() {
            @Override
            public void onSuccess(Boolean info) {
                view.startAct(PhoneSuccessAct.class, null);
                view.finishAllAct();
                view.loading(false);
            }

            @Override
            public void onFailure(Boolean info) {
                view.loading(false);
            }
        });
    }

    @Override
    public void getUserInfo(String id) {
        view.loading(true);
        PersonalCenterParamBean p = new PersonalCenterParamBean();
        p.setCustomId(id);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), new Gson().toJson(p));
        phoneBiz.getUserIncome(requestBody, new BaseBiz.Callback<PersonalCenterBean.DataBean>() {
            @Override
            public void onSuccess(PersonalCenterBean.DataBean info) {
                userPhone = info.getCustom().getPhoneNumber();
                view.loading(false);
            }

            @Override
            public void onFailure(PersonalCenterBean.DataBean info) {
                view.loading(false);
            }
        });
    }
}
